package com.liao.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liao.ticket.common.exception.ErrorCode;
import com.liao.ticket.dao.mapper.AirlineMapper;
import com.liao.ticket.dao.pojo.Airline;
import com.liao.ticket.service.AirlineService;
import com.liao.ticket.util.AdminThreadLocal;
import com.liao.ticket.vo.AirlineVo;
import com.liao.ticket.vo.Result;
import com.liao.ticket.vo.params.AirlineParams;
import com.liao.ticket.vo.params.PageParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AirlineServiceImpl implements AirlineService {
    @Autowired
    private AirlineMapper airlineMapper;

    @Override
    public Result selectOneById(Long id) {
        if(id == null){
            return Result.fail(ErrorCode.USER_AIRLINE_PARAMS_FAIL);
        }
        Airline airline = airlineMapper.selectById(id);
        if(airline == null){
            return Result.fail(ErrorCode.USER_AIRLINE_NO_EXIT);
        }
        return Result.success(copy(airline));
    }

    @Override
    public Result addOne(AirlineParams airlineParams) {
        if (AdminThreadLocal.get() == null) {
            return Result.fail(ErrorCode.USER_NO_POWER);
        }
        String name = airlineParams.getName();
        if(StringUtils.isBlank(name)){
            return Result.fail(ErrorCode.USER_AIRLINE_PARAMS_FAIL);
        }
        if(selectOneByName(name) != null){
            return Result.fail(ErrorCode.USER_AIRLINE_ADD_ALREADY_EXIT);
        }
        Airline airline = new Airline();
        airline.setName(airlineParams.getName());
        airlineMapper.insert(airline);
        return Result.success(copy(airline));
    }

    @Override
    public Result updateOne(AirlineParams airlineParams) {
        if (AdminThreadLocal.get() == null) {
            return Result.fail(ErrorCode.USER_NO_POWER);
        }
        Long id = airlineParams.getId();
        String name = airlineParams.getName();
        if(id == null || StringUtils.isBlank(name)){
            return Result.fail(ErrorCode.USER_AIRLINE_PARAMS_FAIL);
        }
        if(selectOneByName(name) != null){
            return Result.fail(ErrorCode.USER_AIRLINE_UPDATE_ALREADY_EXIT);
        }
        Airline airline = new Airline();
        airline.setId(airlineParams.getId());
        airline.setName(airlineParams.getName());
        int result = airlineMapper.updateById(airline);
        return Result.success(result);
    }

    @Override
    public Result deleteOneById(Long id) {
        if (AdminThreadLocal.get() == null) {
            return Result.fail(ErrorCode.USER_NO_POWER);
        }
        if(id == null){
            return Result.fail(ErrorCode.USER_AIRLINE_PARAMS_FAIL);
        }
        int result = airlineMapper.deleteById(id);
        return Result.success(result);
    }

    @Override
    public Result selectListByName(String name,PageParams pageParams) {
        if (StringUtils.isBlank(name)) {
            return Result.fail(ErrorCode.USER_AIRLINE_PARAMS_FAIL);
        }
        LambdaQueryWrapper<Airline> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(Airline::getName, name);
        Page<Airline> airlinePage = airlineMapper.selectPage(new Page<>(pageParams.getPage(), pageParams.getPageSize()), wrapper);
        List<Airline> airlineList = airlinePage.getRecords();
        return Result.success(copy(airlineList));
    }

    @Override
    public Result selectAll(PageParams pageParams) {
        Page<Airline> airlinePage = airlineMapper.selectPage(new Page<>(pageParams.getPage(), pageParams.getPageSize()), null);
        List<Airline> airlineList = airlinePage.getRecords();
        return Result.success(copy(airlineList));
    }

    private Airline selectOneByName(String name) {
        LambdaQueryWrapper<Airline> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Airline::getName, name);
        return airlineMapper.selectOne(wrapper);
    }

    private AirlineVo copy(Airline airline){
        AirlineVo airlineVo = new AirlineVo();
        BeanUtils.copyProperties(airline, airlineVo);
        return airlineVo;
    }

    private List<AirlineVo> copy(List<Airline> airlineList) {
        List<AirlineVo> airlineVoList = new ArrayList<>();
        airlineList.forEach(item -> airlineVoList.add(copy(item)));
        return airlineVoList;
    }
}
